package com.game.module.backpack.view
{
	import com.dusk.util.McUtil;
	import com.dusk.util.ResUtil;
	import com.game.data.vo.PlayerDataVO;
	import com.game.manager.EventManager;
	import com.game.module.backpack.vo.BackPackEvent;
	import com.game.view.layer.LayerBase;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class BackPackElement extends LayerBase
	{
		
		public function BackPackElement()
		{
			super();
		}
		
		
		private const allPage:uint = 5;
		public var btn_dj:SimpleButton;
		public var btn_zb:SimpleButton;
		public var btn_xhp:SimpleButton;
		private var whichType:int = 1;
		private var meshContainer:Sprite;
		
		private var _curPage:uint = 1;
		private var player:PlayerDataVO;
		private var lastBtn:String = "";
		private var btnState:*;
		
		public function get curPage():uint
		{
			if (_curPage > allPage)
			{
				_curPage = allPage;
			}
			if (_curPage < 1)
			{
				_curPage = 1;
			}
			return _curPage;
		}
		
		public function set curPage(value:uint):void
		{
			if (value > allPage)
			{
				value = allPage;
			}
			if (value < 1)
			{
				value = 1;
			}
			if (_curPage != value)
			{
				_curPage = value;
				refreshBackPack();
			}
		}
		
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("BackPackElement") as MovieClip;
			btn_zb = mc.getChildByName("btn_zb") as SimpleButton;
			btn_dj = mc.getChildByName("btn_dj") as SimpleButton;
			btn_xhp = mc.getChildByName("btn_xhp") as SimpleButton;
			addChild(mc);
		}
		
		override protected function onAdded():void
		{
			btn_dj.addEventListener(MouseEvent.CLICK, selectType);
			btn_zb.addEventListener(MouseEvent.CLICK, selectType);
			btn_xhp.addEventListener(MouseEvent.CLICK, selectType);
			btn_zb.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			EventManager.getIns().registerListen(BackPackEvent.REFRESH_BACKPACK, refreshBackPack);
		}
		
		override protected function onRemoved():void
		{
			btn_dj.removeEventListener(MouseEvent.CLICK, selectType);
			btn_zb.removeEventListener(MouseEvent.CLICK, selectType);
			btn_xhp.removeEventListener(MouseEvent.CLICK, selectType);
			EventManager.getIns().removeListen(BackPackEvent.REFRESH_BACKPACK, refreshBackPack);
			btnState = null;
		}
		
		public function setPlayer(param1:PlayerDataVO):void
		{
			player = param1;
		}
		
		private function drawEquipMesh(param1:Array):void
		{
			var voIdx:int = 0;
			for (var i:int = 0; i < 5; i++)
			{
				for (var j:int = 0; j < 5; j++)
				{
					var packThing:PackThings = new PackThings();
					packThing.x = j * (packThing.width + 11);
					packThing.y = i * (packThing.height + 9);
					meshContainer.addChildAt(packThing, 0);
					if (param1[voIdx + (_curPage - 1) * 25] != undefined)
					{
						packThing.setEquipVO(param1[voIdx + (_curPage - 1) * 25]);
					}
					voIdx++;
				}
			}
		}
		
		private function resetMeshContainer():void
		{
			if (!meshContainer)
			{
				meshContainer = new Sprite();
				meshContainer.x = 0;
				meshContainer.y = 38;
				addChild(meshContainer);
			}
			McUtil.removeAllChildren(meshContainer);
		}
		
		/**
		 * 刷新背包，由全局侦听函数调用
		 */
		private function refreshBackPack():void
		{
			resetMeshContainer();
			switch (whichType)
			{
				case 1:
					drawEquipMesh(player.bagEquipList);
					break;
				case 2:
					drawEquipMesh(player.bagStuffList);
					break;
				case 3:
					drawEquipMesh(player.bugConsumeList);
					break;
			}
		}
		
		private function selectType(param1:MouseEvent):void
		{
			resetMeshContainer();
			switch (param1.currentTarget)
			{
				case btn_zb:
					whichType = 1;
					drawEquipMesh(player.bagEquipList);
					break;
				case btn_dj:
					whichType = 2;
					drawEquipMesh(player.bagStuffList);
					break;
				case btn_xhp:
					whichType = 3;
					drawEquipMesh(player.bugConsumeList);
					break;
			}
			if (btnState)
			{
				if (lastBtn != "")
				{
					this[lastBtn].upState = btnState;
				}
			}
			lastBtn = param1.currentTarget.name;
			btnState = param1.target.upState;
			param1.target.upState = param1.target.downState;
		}
	}
}
